//
//  CustomViewController.m
//  Exchange
//
//  Created by 张庆勇 on 2019/5/7.
//  Copyright © 2019年 张庆勇. All rights reserved.
//

#import "CustomViewController.h"
#import "BottomCustomAlert.h"
typedef void(^animationFinishHandle)(BOOL finished);
@interface CustomViewController ()
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,assign) BOOL keyBoardlsVisible;
@property (weak, nonatomic) id<QVCustomAlertDelegate> delegate;
@property (strong, nonatomic) NSString *className;
@end

@implementation CustomViewController
- (instancetype)initWithCustomContentViewClass:(NSString *)contentViewClass delegate:(id<QVCustomAlertDelegate>)delegate
{
    if (self = [super init]) {
        self.animationDuraction = 0.3;
        self.delegate = delegate;
        self.className = contentViewClass;
    }
    return self;
}
#pragma mark --- UI
- (void)configView:(NSArray *)array
{
    self.contentView = [[BottomCustomAlert alloc] initWithCustomContentDataArray:array];
    NSAssert(self.contentView, @"contentView初始化失败");
    
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wundeclared-selector"
    NSAssert([self.contentView respondsToSelector:@selector(setQvDelegate:)], @"请将contentview继承自QVCustomAlertContentView，以获取属性qvDelegate");
#pragma clang diagnostic pop
    
    [self.contentView setValue:self.delegate forKeyPath:@"qvDelegate"];
    self.view.backgroundColor = RGBA(0, 0, 0, 0.3);
    [self.view addSubview:self.contentView];
}
- (void)show:(NSArray *)array
{
    [self configView:array];
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //    self.window.backgroundColor = [UIColor redColor];
    self.window.rootViewController = self;
    self.window.hidden = NO;
    [self switchAnimationisEnd:NO completion:nil];
    self.isShowing = YES;
    [kWindow addSubview:self.window];
    
}
- (void)switchAnimationisEnd:(BOOL)isEnd completion:(animationFinishHandle)completion
{
    switch (self.direction) {
        case FromTop:
        {
            //TODO:添加contentview从上而下的动画
        }
            break;
        case FromLeft:
        {
            //TODO:添加contentview从左到右的动画
        }
            break;
        case FromRight:
        {
            //TODO:添加contentview从右到左的动画
        }
            break;
        case FromBottom:
        {
            [self showFromBottomisEnd:isEnd completion:completion];
        }
            break;
        case FromCenter:
        {
            //TODO:添加contentview从中间弹出的动画
            [self showFromCenterisEnd:isEnd completion:completion];
        }
            break;
        case NoAnimation:
        {
            
        }
            break;
        default:
            break;
    }
}

- (void)dismiss
{
    WeakSelf
    [self switchAnimationisEnd:YES completion:^(BOOL finished) {
        weakSelf.window.rootViewController = nil;
        [weakSelf.contentView removeFromSuperview];
        weakSelf.isShowing = NO;
        [weakSelf.window removeFromSuperview];
        weakSelf.window.hidden = YES;
        weakSelf.window = nil;
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }];
}
- (void)showFromCenterisEnd:(BOOL)isEndAnimation completion:(animationFinishHandle)completion
{
    self.contentView.center = self.view.center;
    [UIView animateWithDuration:0.0 animations:^{
        
    }completion:^(BOOL finished) {
        if (completion) {
            completion(finished);
        }
    }];
    
    
}

- (void)showFromBottomisEnd:(BOOL)isEndAnimation completion:(animationFinishHandle)completion
{
    WeakSelf
    CGRect frame = self.contentView.frame;
    if (!isEndAnimation) {
        self.contentView.frame = CGRectMake(frame.origin.x, self.view.height, frame.size.width, frame.size.height);
    }
    [UIView animateWithDuration:self.animationDuraction animations:^{
        if (isEndAnimation) {
            weakSelf.contentView.top += weakSelf.contentView.height;
        }else {
            weakSelf.contentView.top -= weakSelf.contentView.height;
        }
    }completion:^(BOOL finished) {
        if (completion) {
            completion(finished);
        }
    }];
}
#pragma mark --- Action
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.225 animations:^{
        [self dismiss];
    }];
}

@end
